(function (angular) {
    'use strict';

    var dialogModule = angular.module('fs.dialog');
    dialogModule.controller('messageDialogsController', messageDialogsController);

    messageDialogsController.$inject = ['$rootScope', '$scope', '$timeout', '$uibModalInstance', '$translate', 'dialogsOptions', 'modalService'];
    function messageDialogsController($rootScope, $scope, $timeout, $uibModalInstance, $translate, dialogsOptions, modalService) {
        var ctrl = this, dialogMessages, resultMessages = [];
        ctrl.title = dialogsOptions.title;
        $rootScope.$uibModalInstance = $uibModalInstance;
        // support the i18n message which has the param {0}, {1}and etc.
        dialogMessages = angular.copy(dialogsOptions.messages);
        angular.forEach(dialogMessages, function (message, index) {
            if (message && angular.isString(message))
                dialogMessages[index] = message.replace(new RegExp("&lt", 'g'), "<").replace(new RegExp("&gt", 'g'), ">");
            var paramObj = {}, args = message.args, msg = message.msg;
            if (msg != undefined && args != undefined) {
                if (!angular.isArray(args)) {
                    var newArray = [];
                    newArray.push(args);
                    args = newArray;
                }
                $translate(args).then(function (objs) {
                    angular.forEach(args, function (key, index) {
                        paramObj['p' + index] = objs[args[index]];
                    });
                    $translate(msg, paramObj).then(function (msg) {
                        resultMessages.push(msg);
                        ctrl.messages = resultMessages;
                    }, function (msg) {
                        resultMessages.push(msg);
                        ctrl.messages = resultMessages;
                    });
                }, function () {
                    $translate(msg, paramObj).then(function (msg) {
                        resultMessages.push(msg);
                        ctrl.messages = resultMessages;
                    }, function (msg) {
                        resultMessages.push(msg);
                        ctrl.messages = resultMessages;
                    });
                });
            } else {
                ctrl.messages = dialogMessages;
            }
        });

        ctrl.redirections = dialogsOptions.redirections;
        if (dialogsOptions.bottomDefaultBtn == undefined)
            dialogsOptions.bottomDefaultBtn = true;
        if (dialogsOptions.bottomOkBtn)
            dialogsOptions.bottomDefaultBtn = false;
        ctrl.dialogsOptions = dialogsOptions;
        ctrl.close = function () {
            if (!dialogsOptions.btnCallback || !dialogsOptions.btnCallback['close'] || dialogsOptions.btnCallback['close']() !== false) {
                $uibModalInstance.dismiss();
            }
        };
        ctrl.ok = function () {
            if (!dialogsOptions.btnCallback || !dialogsOptions.btnCallback['ok'] || dialogsOptions.btnCallback['ok'](true) !== false) {
                $uibModalInstance.close();
            }
        };
        ctrl.promptOk = function () {
            if (!ctrl.promptMessage) {
                modalService.openErrorDialogs({
                    messages: ['title.dialogs.comments.required']
                });
                return;
            }
            if (!dialogsOptions.btnCallback || !dialogsOptions.btnCallback['ok'] || dialogsOptions.btnCallback['ok'](ctrl.promptMessage) !== false) {
                $uibModalInstance.close();
            }
        };
        ctrl.promptOkAndCloseButtons = [{
            "name": "close",
            "label": "button.text.close",
            "type": "button",
            "btnClass": "btn-default",
            "options": {
                "icon": "close",
                "callback": {
                    "click": "vm.close"
                }
            },
            "btnScope": $scope
        }, {
            "name": "promptOk",
            "label": "button.text.ok",
            "type": "button",
            "btnClass": "btn-primary",
            "options": {
                "icon": "ok",
                "callback": {
                    "click": "vm.promptOk"
                }
            },
            "btnScope": $scope
        }

        ];
        ctrl.okAndCloseButtons = [{
            "name": "close",
            "label": "button.text.cancel",
            "type": "button",
            "btnClass": "btn-default",
            "options": {
                "icon": "close",
                "callback": {
                    "click": "vm.close"
                }
            },
            "btnScope": $scope
        }, {
            "name": "ok",
            "label": "button.text.confirm",
            "type": "button",
            "btnClass": "btn-primary",
            "options": {
                "icon": "ok",
                "callback": {
                    "click": "vm.ok"
                }
            },
            "btnScope": $scope
        }];
        ctrl.closeButtons = [{
            "name": "close",
            "label": "button.text.close",
            "type": "button",
            "btnClass": "btn-primary",
            "options": {
                "icon": "close",
                "callback": {
                    "click": "vm.close"
                }
            },
            "btnScope": $scope
        }];
        ctrl.okButtons = [{
            "name": "ok",
            "label": "button.text.ok",
            "type": "button",
            "btnClass": "btn-primary",
            "options": {
                "icon": "ok",
                "callback": {
                    "click": "vm.ok"
                }
            },
            "btnScope": $scope
        }];
        ctrl.redirect = function (redirection) {
            if (redirection.state) {
                $scope.go(redirection.state, redirection.params);
            }
            $uibModalInstance.dismiss();
        };
        ctrl.jump = function (redirection) {
            if (redirection.jump && angular.isFunction(redirection.jump)) {
                redirection.jump();
            } else if (redirection.state) {
                $scope.go(redirection.state, redirection.params);
            }
            $uibModalInstance.dismiss();
        };
        if (dialogsOptions.autoClose === true) {
            $uibModalInstance.rendered.then(function () {
                $timeout(function () {
                    if (!dialogsOptions.cancelAutoClose) {
                        ctrl.close();
                    }
                }, dialogsOptions.autoCloseInterval * 1000, false);
            }, angular.noop);
        }
    }
})(angular);